package com.tianyang.yukuaifu.business.auth.view;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.facebook.drawee.view.SimpleDraweeView;
import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.business.auth.bean.AuthMerchantOutput;
import com.tianyang.yukuaifu.business.auth.bean.BankRegOutput;
import com.tianyang.yukuaifu.business.auth.bean.BusinessOutput;
import com.tianyang.yukuaifu.business.auth.bean.IdCardOutput;
import com.tianyang.yukuaifu.business.auth.bean.MerchantLegalInput;
import com.tianyang.yukuaifu.business.auth.bean.SampleImageOutput;
import com.tianyang.yukuaifu.common.cache.CacheManage;
import com.tianyang.yukuaifu.common.cache.CacheModel;
import com.tianyang.yukuaifu.common.interfaces.CommonConfig;
import com.tianyang.yukuaifu.common.utils.DateCheckPopListener;
import com.tianyang.yukuaifu.module.dialog.DateValidDialog;
import com.tianyang.yukuaifu.module.dialog.SampleImageDialog;
import com.tianyang.yukuaifu.module.dialog.interfaces.IDateValidListener;
import com.tianyang.yukuaifu.module.wheel.DateWheel;
import com.tianyang.yukuaifu.module.wheel.interfaces.IWheelDataChangeCallback;
import com.tianyang.yukuaifu.utils.DateUtils;
import com.tianyang.yukuaifu.utils.SoftInputUtil;
import com.tianyang.yukuaifu.utils.ToastUtil;
import com.tianyang.yukuaifu.utils.VerifyUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Description：小微商户法人信息
 * Created by xyj on 2024/3/10
 */
public class AuthMerchantLegalView extends LinearLayout {

    @BindView(R.id.idCardFrontImgSdv)
    SimpleDraweeView idCardFrontImgSdv;
    @BindView(R.id.idCardBackImgSdv)
    SimpleDraweeView idCardBackImgSdv;
    @BindView(R.id.handImgSdv)
    SimpleDraweeView handImgSdv;
    @BindView(R.id.bankFrontImgSdv)
    SimpleDraweeView bankFrontImgSdv;
    @BindView(R.id.xinmingEt)
    EditText xinmingEt;
    @BindView(R.id.shenfengidEt)
    EditText shenfengidEt;
    @BindView(R.id.shenfengbegindateTv)
    TextView shenfengbegindateTv;
    @BindView(R.id.shenfengenddateTv)
    TextView shenfengenddateTv;
    @BindView(R.id.lawyerAddressEt)
    EditText lawyerAddressEt;
    @BindView(R.id.bankNameEt)
    EditText bankNameEt;
    @BindView(R.id.openingBankTv)
    TextView openingBankTv;
    @BindView(R.id.bankcardidEt)
    EditText bankcardidEt;
    @BindView(R.id.bankTelEt)
    EditText bankTelEt;
    @BindView(R.id.confirmTv)
    Button confirmTv;

    private SampleImageDialog vSampleImageDialog;

    private DateValidDialog vDateValidDialog;

    private DateCheckPopListener mDateStartPopListener;

    private DateCheckPopListener mDateEndPopListener;

    private MerchantLegalInput mMerchantLegalInput;

    private SampleImageOutput mOutput;

    public AuthMerchantLegalView(Context context, AttributeSet attrs) {
        super(context, attrs);
        View.inflate(getContext(), R.layout.view_auth_merchant_legal, this);
        ButterKnife.bind(this);
        mMerchantLegalInput = new MerchantLegalInput();
        mOutput = CacheManage.getInstance().getCache(CacheModel.SAMPLE_IMAGE);
    }

    @OnClick({R.id.shenfengbegindateTv, R.id.shenfengenddateTv, R.id.takeIdCardFrontTv,
            R.id.takeIdCardBackTv, R.id.takeHandTv, R.id.takeBankFrontTv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.shenfengbegindateTv:
                if (mDateStartPopListener == null) {
                    mDateStartPopListener = new DateCheckPopListener((Activity) getContext(), true);
                }
                mDateStartPopListener.handleRegionCheck(view, "请选择有效期开始日期", shenfengbegindateTv, new IWheelDataChangeCallback() {
                    @Override
                    public void wheelDataChangeCallback(String wheelData) {
                        if (!TextUtils.isEmpty(wheelData)) {
                            mMerchantLegalInput.setShenfengbegindate(wheelData);
                        }
                    }
                });
                break;
            case R.id.shenfengenddateTv:
                if (vDateValidDialog == null) {
                    vDateValidDialog = new DateValidDialog(getContext());
                }
                vDateValidDialog.setListener(new IDateValidListener() {
                    @Override
                    public void onDateValid(boolean isLongTerm) {
                        if (isLongTerm) {
                            shenfengenddateTv.setText("长期");
                            mMerchantLegalInput.setShenfengenddate(CommonConfig.DATE_LONG_TERM);
                        } else {
                            view.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    showEndValidDialog(view);
                                }
                            }, 100);
                        }
                    }
                });
                vDateValidDialog.show();
                break;
            case R.id.takeIdCardFrontTv:
                if (mOutput != null) {
                    showSampleImageDialog(mOutput.getShenfengzhengpic());
                }
                break;
            case R.id.takeIdCardBackTv:
                if (mOutput != null) {
                    showSampleImageDialog(mOutput.getShenfengfanpic());
                }
                break;
            case R.id.takeHandTv:
                if (mOutput != null) {
                    showSampleImageDialog(mOutput.getHoldingCardPic());
                }
                break;
            case R.id.takeBankFrontTv:
                if (mOutput != null) {
                    showSampleImageDialog(mOutput.getBankcardpic());
                }
                break;
        }
    }

    private void showSampleImageDialog(SampleImageOutput.SampleImage sampleImage) {
        if (vSampleImageDialog == null) {
            vSampleImageDialog = new SampleImageDialog(getContext());
        }
        vSampleImageDialog.showPictureDataView(sampleImage);
        vSampleImageDialog.show();
    }

    private void showEndValidDialog(View view) {
        if (mDateEndPopListener == null) {
            mDateEndPopListener = new DateCheckPopListener((Activity) getContext(), true);
        }
        mDateEndPopListener.handleRegionCheck(view, "请选择有效期结束日期", shenfengenddateTv, DateWheel.DATE_TYPE_ALL,
                new IWheelDataChangeCallback() {
                    @Override
                    public void wheelDataChangeCallback(final String wheelData) {
                        if (!TextUtils.isEmpty(wheelData)) {
                            mMerchantLegalInput.setShenfengbegindate(wheelData);
                        }
                    }
                });
    }

    public void onFeedBackView(AuthMerchantOutput output) {
        if (output != null) {
            mMerchantLegalInput = JSON.parseObject(JSON.toJSONString(output), MerchantLegalInput.class);
            if (mMerchantLegalInput != null) {
                if (!TextUtils.isEmpty(mMerchantLegalInput.getShenfengzhengpic()))
                    idCardFrontImgSdv.setImageURI(Uri.parse(mMerchantLegalInput.getShenfengzhengpic()));
                if (!TextUtils.isEmpty(mMerchantLegalInput.getShenfengfanpic()))
                    idCardBackImgSdv.setImageURI(Uri.parse(mMerchantLegalInput.getShenfengfanpic()));
                if (!TextUtils.isEmpty(mMerchantLegalInput.getHoldingCardPic()))
                    handImgSdv.setImageURI(Uri.parse(mMerchantLegalInput.getHoldingCardPic()));
                if (!TextUtils.isEmpty(mMerchantLegalInput.getBankcardpic()))
                    bankFrontImgSdv.setImageURI(Uri.parse(mMerchantLegalInput.getBankcardpic()));
                xinmingEt.setText(VerifyUtil.checkString(mMerchantLegalInput.getXinming()));
                shenfengidEt.setText(VerifyUtil.checkString(mMerchantLegalInput.getShenfengid()));
                shenfengbegindateTv.setText(VerifyUtil.checkString(mMerchantLegalInput.getShenfengbegindate()));
                if (TextUtils.equals(CommonConfig.DATE_LONG_TERM, mMerchantLegalInput.getShenfengenddate())) {
                    shenfengenddateTv.setText("长期");
                } else {
                    shenfengenddateTv.setText(VerifyUtil.checkString(mMerchantLegalInput.getShenfengenddate()));
                }
                lawyerAddressEt.setText(VerifyUtil.checkString(mMerchantLegalInput.getLawyerAddress()));
                bankNameEt.setText(VerifyUtil.checkString(mMerchantLegalInput.getBankName()));
                openingBankTv.setText(VerifyUtil.checkString(mMerchantLegalInput.getOpeningBank()));
                bankcardidEt.setText(VerifyUtil.checkString(mMerchantLegalInput.getBankcardid()));
                bankTelEt.setText(VerifyUtil.checkString(mMerchantLegalInput.getBankTel()));
            } else {
                mMerchantLegalInput = new MerchantLegalInput();
            }
        }
    }

    public void showBusinessView(BusinessOutput output) {
        if (output != null) {
            openingBankTv.setText(VerifyUtil.checkString(output.getBranchBankName()));
            mMerchantLegalInput.setOpeningBank(output.getBranchBankName());
            mMerchantLegalInput.setBranchBankNo(output.getBranchNo());
        }
    }

    public void showIdCardInfoView(IdCardOutput output) {
        if (output != null) {
            if (TextUtils.equals("1", output.getCodeType())) {
                xinmingEt.setText(VerifyUtil.checkString(output.getName()));
                mMerchantLegalInput.setXinming(output.getName());
                shenfengidEt.setText(VerifyUtil.checkString(output.getCode()));
                mMerchantLegalInput.setShenfengid(output.getCode());
                lawyerAddressEt.setText(VerifyUtil.checkString(output.getAddress()));
                mMerchantLegalInput.setLawyerAddress(output.getAddress());
            } else if (TextUtils.equals("2", output.getCodeType())) {
                if (!TextUtils.isEmpty(output.getIssueDate())) {
                    String issueDate = DateUtils.formatTime(output.getIssueDate(), "yyyyMMdd", "yyyy-MM-dd");
                    shenfengbegindateTv.setText(issueDate);
                    mMerchantLegalInput.setShenfengbegindate(issueDate);
                }
                if (!TextUtils.isEmpty(output.getExpiryDate())) {
                    String expiryDate = DateUtils.formatTime(output.getExpiryDate(), "yyyyMMdd", "yyyy-MM-dd");
                    shenfengenddateTv.setText(expiryDate);
                    mMerchantLegalInput.setShenfengenddate(expiryDate);
                }
            }
        }
    }

    public void showBankCardInfoView(BankRegOutput output) {
        if (output != null) {
            bankNameEt.setText(VerifyUtil.checkString(output.getBank_name()));
            mMerchantLegalInput.setBankName(output.getBank_name());
            if (!TextUtils.isEmpty(output.getBank_card_number())) {
                String bankCardId = output.getBank_card_number().replaceAll(" ", "");
                bankcardidEt.setText(bankCardId);
                mMerchantLegalInput.setBankcardid(bankCardId);
            }
        }
    }

    public void showPhotoView(String urlStr, String photoType) {
        switch (photoType) {
            case CommonConfig.TYPE_IDFROPIC:
                idCardFrontImgSdv.setImageURI(Uri.parse(urlStr));
                mMerchantLegalInput.setShenfengzhengpic(urlStr);
                break;
            case CommonConfig.TYPE_IDCONPIC:
                idCardBackImgSdv.setImageURI(Uri.parse(urlStr));
                mMerchantLegalInput.setShenfengfanpic(urlStr);
                break;
            case CommonConfig.TYPE_IDHANDPIC:
                handImgSdv.setImageURI(Uri.parse(urlStr));
                mMerchantLegalInput.setHoldingCardPic(urlStr);
                break;
            case CommonConfig.TYPE_CRDFROPIC:
                bankFrontImgSdv.setImageURI(Uri.parse(urlStr));
                mMerchantLegalInput.setBankcardpic(urlStr);
                break;
        }
    }

    public MerchantLegalInput getMerchantLegalInput() {
        mMerchantLegalInput.setXinming(xinmingEt.getText().toString());
        mMerchantLegalInput.setShenfengid(shenfengidEt.getText().toString());
        mMerchantLegalInput.setLawyerAddress(lawyerAddressEt.getText().toString());
        mMerchantLegalInput.setBankName(bankNameEt.getText().toString());
        mMerchantLegalInput.setBankcardid(bankcardidEt.getText().toString());
        mMerchantLegalInput.setBankTel(bankTelEt.getText().toString());
        return mMerchantLegalInput;
    }

    public boolean checkMerchantLegal() {
        if (TextUtils.isEmpty(mMerchantLegalInput.getShenfengzhengpic())) {
            ToastUtil.show("请上传身份证人像面");
            return false;
        }
        if (TextUtils.isEmpty(mMerchantLegalInput.getShenfengfanpic())) {
            ToastUtil.show("请上传身份证国徽面");
            return false;
        }
        if (TextUtils.isEmpty(mMerchantLegalInput.getHoldingCardPic())) {
            ToastUtil.show("请上传手持身份证照");
            return false;
        }
        if (TextUtils.isEmpty(mMerchantLegalInput.getBankcardpic())) {
            ToastUtil.show("请上传银行卡卡号照");
            return false;
        }
        if (TextUtils.isEmpty(xinmingEt.getText())) {
            ToastUtil.show("请输入证件姓名");
            SoftInputUtil.openKeybord(xinmingEt, getContext());
            return false;
        }
        if (TextUtils.isEmpty(shenfengidEt.getText())) {
            ToastUtil.show("请输入证件号码");
            SoftInputUtil.openKeybord(shenfengidEt, getContext());
            return false;
        }
        if (TextUtils.isEmpty(mMerchantLegalInput.getShenfengbegindate())) {
            ToastUtil.show("请选择证件有效期始");
            return false;
        }
        if (TextUtils.isEmpty(mMerchantLegalInput.getShenfengenddate())) {
            ToastUtil.show("请选择证件有效期至");
            return false;
        }
        if (TextUtils.isEmpty(lawyerAddressEt.getText())) {
            ToastUtil.show("请填写身份证地址");
            SoftInputUtil.openKeybord(lawyerAddressEt, getContext());
            return false;
        }
        if (TextUtils.isEmpty(bankNameEt.getText())) {
            ToastUtil.show("请填写银行名称");
            SoftInputUtil.openKeybord(bankNameEt, getContext());
            return false;
        }
        if (TextUtils.isEmpty(mMerchantLegalInput.getOpeningBank())) {
            ToastUtil.show("请选择开户支行");
            return false;
        }
        if (TextUtils.isEmpty(bankcardidEt.getText())) {
            ToastUtil.show("请填写银行卡号");
            SoftInputUtil.openKeybord(bankcardidEt, getContext());
            return false;
        }
        if (TextUtils.isEmpty(bankTelEt.getText())) {
            ToastUtil.show("请填写预留手机号");
            SoftInputUtil.openKeybord(bankTelEt, getContext());
            return false;
        } else if (VerifyUtil.checkPhoneNumber(bankTelEt.getText())) {
            ToastUtil.show("请输入正确的手机号");
            SoftInputUtil.openKeybord(bankTelEt, getContext());
            return false;
        }
        return true;
    }

    public void setOnlyReadView() {
        xinmingEt.setEnabled(false);
        shenfengidEt.setEnabled(false);
        shenfengbegindateTv.setEnabled(false);
        shenfengbegindateTv.setCompoundDrawables(null, null, null, null);
        shenfengenddateTv.setEnabled(false);
        shenfengenddateTv.setCompoundDrawables(null, null, null, null);
        lawyerAddressEt.setEnabled(false);
        bankNameEt.setEnabled(false);
        openingBankTv.setEnabled(false);
        openingBankTv.setCompoundDrawables(null, null, null, null);
        bankcardidEt.setEnabled(false);
        bankTelEt.setEnabled(false);
        confirmTv.setVisibility(GONE);
    }

    public void onDestroyView() {
        if (vSampleImageDialog != null && vSampleImageDialog.isShowing()) {
            vSampleImageDialog.dismiss();
            vSampleImageDialog = null;
        }
        if (vDateValidDialog != null) {
            vDateValidDialog.dismiss();
            vDateValidDialog = null;
        }
    }
}
